package com.are.odisea;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SrvCategorias
 */
@WebServlet("/SrvCategorias")
public class SrvCategorias extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SrvCategorias() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void ProcesarPeticion(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	response.setContentType("text/html;charset=ISO-8859-1");
    	PrintWriter out = response.getWriter();	

    	Categorias gc=null;   	
    	db conexion = null;
    	try {
			conexion = new db();
			gc = new Categorias(conexion);
			ResultSet rscp=gc.GetCatPadres();
			while (rscp.next()){
				out.print("<OPTGROUP label='"+rscp.getString(2)+"'>");
				ResultSet rsch=gc.GetCatHijas(rscp.getString(1));
				while (rsch.next()){
					out.print("<option  value="+rsch.getString(1)+">"+rsch.getString(2)+"</option>");
				}
				out.print("</OPTGROUP>");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (conexion != null){
				try {
					conexion.Close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					//out.print("NO se conecto a la base de datps");
					e.printStackTrace();
				}
			}
		}
    	
    	
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ProcesarPeticion(request, response); 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ProcesarPeticion(request, response); 
	}

}
